package com.djakapm.markdown.editor;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static org.junit.Assert.assertTrue;

public class SpecForEngine {
    private  Engine engine;

    @Before
    public void setUp() {
        this.engine = new Engine();
    }

    @After
    public void tearDown() {
        this.engine = null;
    }


    @Test
    public void can_open_file(){
        File file = new File("C:\\Users\\Djaka\\Documents\\markdown_test_document.draft");
        assertTrue("Should be able to open a file",!"".equals(this.engine.openDocument(file)));
    }

    @Test
    public void can_save_file_without_ext(){
        File inputFile;
        File outputFile;

        inputFile = new File("tmp\\markdown_test_document.draft");
        this.engine.openDocument(inputFile);
        assertTrue("Should be able to save a file without extension",this.engine.saveContent("tmp\\file_without_ext","This is sparta","<p>This is sparta</p>") != null);

        outputFile = new File("tmp\\file_without_ext.draft");
        try {
            assertTrue("The content of the file that saved must be the same", FileUtils.contentEquals(inputFile,outputFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void can_save_file_with_ext(){
        File inputFile;
        File outputFile;

        inputFile = new File("tmp\\markdown_test_document.draft");
        this.engine.openDocument(inputFile);
        assertTrue("Should be able to save a file without extension",this.engine.saveContent("tmp\\file_with_ext.draft","This is sparta","<p>This is sparta</p>") != null);

        outputFile = new File("tmp\\file_with_ext.draft");

        try {
            assertTrue("The content of the file that saved must be the same", FileUtils.contentEquals(inputFile,outputFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void can_convert_markdown_string_to_html_string() {
        StringBuffer markupStr = new StringBuffer();
        String markdownStr;
        String targetStr = "<p><a href=\"http://www.microsoft.com/express/downloads/\" title=\"Visual Studio C# 2010 Express\">Visual Studio C# 2010 Express</a></p>\n";

        markupStr.append("[vs2010-link]: http://www.microsoft.com/express/downloads/ \"Visual Studio C# 2010 Express\"\n");
        markupStr.append("[Visual Studio C# 2010 Express][vs2010-link]");

        markdownStr = engine.convert(markupStr.toString());
        assertTrue("Conversion string must be correct",targetStr.equals(markdownStr));
    }
}